package com.zhou.common.mybatis.template.shiro;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.flowable.cmmn.api.CmmnRuntimeService;
import org.flowable.cmmn.api.CmmnTaskService;
import org.flowable.cmmn.api.runtime.PlanItemInstance;
import org.flowable.cmmn.api.runtime.UserEventListenerInstance;
import org.flowable.cmmn.api.runtime.UserEventListenerInstanceQuery;
import org.flowable.cmmn.engine.CmmnEngine;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class CMMNTest5 implements CommandLineRunner {

    @Autowired
    private CmmnEngine  cmmnEngine;
    @Override
    public void run(String... args) throws Exception {
        CmmnRuntimeService cmmnRuntimeService = cmmnEngine.getCmmnRuntimeService();
        CmmnTaskService cmmnTaskService = cmmnEngine.getCmmnTaskService();

//        UserEventListenerInstanceQuery userEventListenerInstanceQuery = cmmnRuntimeService.createUserEventListenerInstanceQuery();

        Map<String,Object> map= Maps.newHashMap();
        map.put("contory_person","contory_person1");

        /*CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
                .caseDefinitionKey("cmmn_test4")
                .variables(map)
                .start();*/
        String caseId="d8a04cc0-c232-11ec-af3f-5414f3dd8146";

        map.put("contory_person","contory_person3");
        map.put("street_person","street_person21");
        cmmnRuntimeService.setVariables(caseId,map);



        List<PlanItemInstance> planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery()
                .caseInstanceId(caseId)
                .orderByName().asc()
                .list();
        for (PlanItemInstance planItemInstance : planItemInstances) {
            System.out.println(planItemInstance.getName()
                    + ", state=" + planItemInstance.getState()

                    + ", parent stage=" + planItemInstance.getStageInstanceId());
        }

        UserEventListenerInstanceQuery userEventListenerInstanceQuery = cmmnRuntimeService.createUserEventListenerInstanceQuery()
                .name("街道分派任务")
                ;
        List<UserEventListenerInstance> list = userEventListenerInstanceQuery.list();
        for (UserEventListenerInstance userEventListenerInstance : list) {
            System.out.println(userEventListenerInstance.getName()
                    + ", state=" + userEventListenerInstance.getState()
                    + ", parent stage=" + userEventListenerInstance.getStageInstanceId());
            //cmmnRuntimeService.completeUserEventListenerInstance(userEventListenerInstance.getId() );
        }
        //cmmnRuntimeService.completeUserEventListenerInstance(list.get(1).getId() );
        planItemInstances = cmmnRuntimeService.createPlanItemInstanceQuery()
                .caseInstanceId(caseId)
                .orderByName().asc()
                .list();
        for (PlanItemInstance planItemInstance : planItemInstances) {
            System.out.println(planItemInstance.getName()
                    + ", state=" + planItemInstance.getState()
                    + ", parent stage=" + planItemInstance.getStageInstanceId());
        }
        List<Task> employeeTasks = cmmnTaskService.createTaskQuery()
                .caseDefinitionKey("cmmn_test4")
                .caseInstanceId(caseId)
                .taskName("县审批任务")
                .orderByTaskName().asc()
                .list();
        for (Task task : employeeTasks) {
            System.out.println("Task for employee: " + task);
        }
        cmmnTaskService.complete(employeeTasks.get(0).getId());
    }
}
